package com.beetlestech.nfe.main.biz.com.impl;
import java.sql.Date;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.type.AutoInject;
import com.beetlestech.nfe.fw.core.util.CollectionUtil;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbComblcmMaster;
import com.beetlestech.nfe.main.biz.com.HbUser;
import com.beetlestech.nfe.main.biz.com.HbUserApp;
import com.beetlestech.nfe.main.biz.com.dto.UserInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.ApploginlgDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.TloginlgDTO;
import com.beetlestech.nfe.main.db.tbl.UserjurisdictionDTO;

/**
 * 職員情報取得Bizロジック
 *
 * 変更履歴
 * REV.         変更日付           変更者              変更内容
 * -------      ---------------    ----------------    ------------------
 * 1.0          2015/08/14         GCLI          新規作成
*/
public class HbUserAppImpl extends AbstractDBLogic implements HbUserApp, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	/**
	 * ビジネスロジック設定マスター Bizロジック
	 */
	@AutoInject
	protected HbComblcmMaster hbComblcmMaster;

	/**
	 * 職員情報データ
	 */
	protected static final String SH_INFO = "職員情報データ";

	@Override
	public UserInfoDTO getLoginUserInfo(
			String loginName, String password,String sesID) {

		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusername(loginName);
		userDTO.setphone(loginName);
		userDTO.setemail(loginName);
		List<MstusrDTO> userDTOs = queryDAO.queryForObjectList(SQLID.LOGIN_USRE_EQUAL_SEARCH, userDTO);
		
		UserInfoDTO userInfo = null;
		if (CollectionUtil.isEmpty(userDTOs)) {
			//検出件数が0件の場合
			createApplicationException(M00010WC, SH_INFO);
		} else if (userDTOs.size() > 1) {
			//処理１で2件以上取得できた場合は、「ApplicationException」を返す
			createApplicationException(M10190WS, SH_INFO);
		} else {
			userInfo = setUserInfo(userDTOs.get(0));
			if (null != password && "" != password && ! password.equals(userInfo.getpassword())) {
				createApplicationException(M00010WC, SH_INFO);
			}
			if(sesID==null){
				sesID="jsp";
			}
			recordLogin(userInfo.getusrid().toString(),sesID);
		}

		//検出データを返す
		return userInfo;
	}
	public void recordLogin(
			String loginId,
			String sessionId) {
		// ログイン情報を設定する
		ApploginlgDTO apploginlgDTO = ApploginlgDTO.setDefaultValue();
		apploginlgDTO.setLoginId(loginId);
		
		apploginlgDTO.setSessionId(sessionId);
		apploginlgDTO.setEventDate(getCurrentDate());
		apploginlgDTO.setEventTime(getCurrentTime());
		// LOGONLGにログイン情報を挿入する
		updateDAO.updateImmediately(ApploginlgDTO.DELETE, apploginlgDTO);
		updateDAO.updateImmediately(ApploginlgDTO.INSERT, apploginlgDTO);
	}
	private UserInfoDTO setUserInfo(MstusrDTO mstusrDTO){
		UserInfoDTO userInfo = new UserInfoDTO();
		userInfo.setfirstloginflg(mstusrDTO.getfirstloginflg());
		userInfo.setpassword(mstusrDTO.getpassword());
		userInfo.setstate(mstusrDTO.getstate());
		userInfo.settype(mstusrDTO.gettype());
		userInfo.setusername(mstusrDTO.getusername());
		userInfo.setusrid(mstusrDTO.getusrid());
		userInfo.setEmail(mstusrDTO.getemail());
		userInfo.setPhone(mstusrDTO.getphone());
		
		return userInfo;
	}
	

	
}
